package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.adapter.PopLvAdapter;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.GsonUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.UiHelper;
import com.ycsj.goldmedalnewconcept.view.RoundImageView;





import org.json.JSONObject;
import org.w3c.dom.NameList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 创建日期：2017/11/10  13:31
 * 描述:创建小组的Activity
 * 作者:张伟
 */

@SuppressLint("NewApi")
public class CreateTeamActivity extends Activity implements View.OnClickListener {

    //标题
    private TextView tvTitleName;

    //返回
    private LinearLayout llBack;

    //创建
    private TextView tvCreate;


    //头像
    private RelativeLayout rlHead;

    //小组图标
    private RoundImageView lvHead;

    //小组名称
    private RelativeLayout rlXzmc;

    //小组名称,口号,目标,音乐
    private TextView tvXzmc, tvXzkh, tvXzmb, tvXzyy;

    //小组口号
    private RelativeLayout rlXzkh;

    //小组目标
    private RelativeLayout rlXzmb;

    //小组音乐
    private RelativeLayout rlXzyy;

    //用于记录小组图片的集合
    private List<Integer> teamLists;

    //用于保存小组名称的集合
    private List<String> teamNames;

    //用户头像选择的PopUpWindow相关
    private PopupWindow popupWindow;
    private View headView;
    private ImageView lvDelete;
    private ImageView lvLeft;
    private ImageView lvRight;
    private ImageView lvTeam;
    private TextView tvSave;


    //修改小组信息时传递过来的teamId
    private String teamId;

    //用户ID
    private String userID;

    //修改小组信息时候传递过来的teamNum;
    private String teamNum;
    private String classNum;

    //用于记录小组头像,上传给服务器的小组头像的值,注意回传的时候+1处理
    private int icon = -1;

    //用于记录修改的值
    private int newIcon;

    //用于记录小组名称,上传给服务器
    private String xzmc;

    private String newXzmc;


    //用于记录小组口号,上传给服务器
    private String xzkh;
    private String newXzkh;

    //用于记录小组目标
    private String xzmb;
    private String newXzmb;


    //用于记录小组音乐的索引
    private int music = -1;
    private int newMusic = -1;


    //用于记录是创建新小组还是修改小组,200是创建新小组,100是修改小组
    private int flag;


    //切换图片的位置索引值
    private int pos = 0;

    //用于切换组头像的图片集合
    private int[] icons = {R.drawable.group1, R.drawable.group2, R.drawable.group3
            , R.drawable.group4, R.drawable.group5, R.drawable.group6, R.drawable.group7, R.drawable.group8, R.drawable.group9
            , R.drawable.group10};

    //弹出通用PopUpWindow的相关
    private View commonView;
    private TextView tvTitle;
    private ListView lvCommon;
    private PopLvAdapter popLvAdapter;
    private TextView tvCancel;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_team);

        //设置状态栏
        setBar();

        //初始化View
        initView();

        //获取已经使用的小组图标的标记集合
        getData();

        //设置监听
        initListener();

    }

    /**
     * 初始化View的操作
     */
    private void initView() {
        tvTitleName = (TextView) findViewById(R.id.tv_title);
        llBack = (LinearLayout) findViewById(R.id.ll_back);
        tvCreate = (TextView) findViewById(R.id.tv_create);
        rlHead = (RelativeLayout) findViewById(R.id.rl_head);
        rlXzmc = (RelativeLayout) findViewById(R.id.rl_xzmc);
        rlXzkh = (RelativeLayout) findViewById(R.id.rl_xzkh);
        rlXzmb = (RelativeLayout) findViewById(R.id.rl_xzmb);
        rlXzyy = (RelativeLayout) findViewById(R.id.rl_xzyy);
        tvXzmc = (TextView) findViewById(R.id.tv_xzmc);
        tvXzkh = (TextView) findViewById(R.id.tv_xzkh);
        tvXzmb = (TextView) findViewById(R.id.tv_xzmb);
        tvXzyy = (TextView) findViewById(R.id.tv_xzyy);
        lvHead = (RoundImageView) findViewById(R.id.lv_head);
    }


    /**
     * 设计监听的方法
     */
    private void initListener() {
        llBack.setOnClickListener(this);
        rlHead.setOnClickListener(this);
        rlXzmc.setOnClickListener(this);
        rlXzkh.setOnClickListener(this);
        rlXzmb.setOnClickListener(this);
        rlXzyy.setOnClickListener(this);
        tvCreate.setOnClickListener(this);
    }

    /**
     * 获取已经使用的小组图标
     */
    public void getData() {
        //获取已经选择过的小组头像
        String teamJson = getIntent().getStringExtra("team");
        if (!TextUtils.isEmpty(teamJson)) {
            teamLists = GsonUtil.jsonToList(teamJson, Integer[].class);
        } else {
            teamLists = new ArrayList<Integer>();
        }


        //获取已经使用过的小组名称信息
        String teamNameJson = getIntent().getStringExtra("teamNameList");
        if (!TextUtils.isEmpty(teamNameJson)) {
            teamNames = GsonUtil.jsonToList(teamNameJson, String[].class);
        } else {
            teamNames = new ArrayList<>();
        }

        //班级的编号
        classNum = getIntent().getStringExtra("classnum");

        flag = getIntent().getIntExtra("flag", -1);

        if (flag == 200) {
            //表示是创建新的小组
            tvTitleName.setText("创建小组");
            tvCreate.setText("创建");
        } else if (flag == 100) {
            //表示是修改小组相关信息className
            tvTitleName.setText(getIntent().getStringExtra("className"));


            //获取小组的teamID
            teamId = getIntent().getStringExtra("teamid");

            //获取小组的teamNum
            teamNum = getIntent().getStringExtra("teamnum");

            //如果是修改小组信息,需要设置标题,小组头像,名称,口号,目标
            icon = Integer.parseInt(getIntent().getStringExtra("teamicon"));//记录当前小组的icon头像值
            newIcon = Integer.parseInt(getIntent().getStringExtra("teamicon"));//记录当前小组的icon头像值

            String icon = getIntent().getStringExtra("teamicon");//小组头像
            if ("1".equals(icon)) {
                lvHead.setImageResource(R.drawable.group1);
            } else if ("2".equals(icon)) {
                lvHead.setImageResource(R.drawable.group2);
            } else if ("3".equals(icon)) {
                lvHead.setImageResource(R.drawable.group3);
            } else if ("4".equals(icon)) {
                lvHead.setImageResource(R.drawable.group4);
            } else if ("5".equals(icon)) {
                lvHead.setImageResource(R.drawable.group5);
            } else if ("6".equals(icon)) {
                lvHead.setImageResource(R.drawable.group6);
            } else if ("7".equals(icon)) {
                lvHead.setImageResource(R.drawable.group7);
            } else if ("8".equals(icon)) {
                lvHead.setImageResource(R.drawable.group8);
            } else if ("9".equals(icon)) {
                lvHead.setImageResource(R.drawable.group9);
            } else if ("10".equals(icon)) {
                lvHead.setImageResource(R.drawable.group10);
            }
            tvXzmc.setText(getIntent().getStringExtra("className"));//小组名称

            tvXzkh.setText(getIntent().getStringExtra("teamslogan"));//小组口号

            tvXzmb.setText(getIntent().getStringExtra("teamtarget"));//小组目标

            tvXzyy.setText(getIntent().getStringExtra("teammusic"));//小组音乐

            xzmc = getIntent().getStringExtra("className");//记录小组名称的值
            newXzmc = getIntent().getStringExtra("className");//记录小组名称的值
            xzkh = getIntent().getStringExtra("teamslogan");//记录小组口号的值
            newXzkh = getIntent().getStringExtra("teamslogan");//记录小组口号的值
            xzmb = getIntent().getStringExtra("teamtarget");//小组目标的值
            newXzmb = getIntent().getStringExtra("teamtarget");//小组目标的值
            music = Integer.parseInt(getIntent().getStringExtra("teammusic"));//记录小组音乐的值
            newMusic = Integer.parseInt(getIntent().getStringExtra("teammusic"));//记录小组音乐的值
            tvCreate.setText("修改");
        }
    }

    /**
     * 页面控件的点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                //返回
                finish();
                break;
            case R.id.tv_create:
                //创建
                judgeAll();
                break;
            case R.id.rl_head:
                //小组头像
                showPopHead();
                break;
            case R.id.rl_xzmc:
                //小组名称
                List<String> items = new ArrayList<>();
                items.add("智者队");
                items.add("勇者队");
                items.add("欢乐队");
                items.add("博爱队");
                items.add("自定义");
                showPopCommon(0, "小组名称", items);
                break;
            case R.id.rl_xzkh:
                //小组口号
                List<String> xzkh = new ArrayList<>();
                xzkh.add("团结一心，其利断金");
                xzkh.add("披荆斩棘，剑指第一");
                xzkh.add("激情飞扬，只会无限");
                xzkh.add("力争上游，永不言败");
                xzkh.add("自定义");
                showPopCommon(1, "小组口号", xzkh);
                break;
            case R.id.rl_xzmb:
                //小组目标
                List<String> xzmb = new ArrayList<>();
                xzmb.add("boss是我们的！");
                xzmb.add("这个第一被我们承包啦！");
                xzmb.add("快乐比拼，开心学习");
                xzmb.add("我们的目标是：赢");
                xzmb.add("自定义");
                showPopCommon(2, "小组目标", xzmb);
                break;
            case R.id.rl_xzyy:
                //小组音乐
                Intent intent = new Intent(this, TeamMusicActivity.class);
                startActivityForResult(intent, 0X140);
                break;
            case R.id.lv_left:
                //点击左箭头
                syncLeftState();
                break;
            case R.id.lv_right:
                //点击右边箭头
                syncRightState();
                break;
            case R.id.tv_save:
                //点击保存的操作
                if (teamLists.contains((pos + 1))) {
                    Toast.makeText(this, "小组头像已经被使用", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    //保存pos的位置(注意+1),同时回显小组头像到界面,关闭头像的PopUpWindow
                    lvHead.setImageResource(icons[pos]);

                    //记录上传给服务器的icon
                    icon = pos + 1;
                    //关闭PopUpWindow
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                        popupWindow = null;
                        headView = null;
                    }
                }
                break;
            case R.id.tv_cancel:
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    commonView = null;
                }
                break;
        }
    }

    /**
     * 点击创建后判断上传给服务器资料的完整性 ,然后进行服务器上传的操作
     */
    private void judgeAll() {
        if (icon == -1) {
            //说明没有选择小组头像
            Toast.makeText(this, "请选择小组头像", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(xzmc)) {
            //说明没有选择小组名称
            Toast.makeText(this, "请选择小组名称", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(xzkh)) {
            //说明没有选择小组口号
            Toast.makeText(this, "请选择小组口号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(xzmb)) {
            //说明没有选择小组目标
            Toast.makeText(this, "请选择小组目标", Toast.LENGTH_SHORT).show();
            return;
        }

        if (music == -1) {
            //说明没有选择小组名称
            Toast.makeText(this, "请选择小组音乐", Toast.LENGTH_SHORT).show();
            return;
        }
        
        if (flag == 200) {
        if (teamNames.contains(xzmc)) {
        	//说明没有选择小组名称
        	Toast.makeText(this, "小组名称不能重复", Toast.LENGTH_SHORT).show();
        	return;
        } 
        }

        //弹框进行网络请求
        if (flag == 200) {
            //创建新小组
            UiHelper.showDialog(this, "创建中...");
            httpCreateTeam();
        } else if (flag == 100) {
            if (hasUpdateTeam()) {
                UiHelper.showDialog(this, "修改中...");
                httpUpdate();
            } else {
                //用户没有修改小组信息,直接finish掉当前页面
                finish();
            }
        }
    }

    /**
     * 修改小组信息请求服务器
     * 需要上传给服务器的参数有:type(7),id(对应的小组的teamid),money(0),exp(0),userid(用户登陆id)
     * classnum(班级编号),icon(小组头像),nam(小组名称),solgan(小组口号),targe(小组目标),music(小组音乐)
     * num(小组的num)
     */
    private void httpUpdate() {
        OkHttpClient mOkHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("type", "7");
        builder.add("id", teamId + "");
        builder.add("money", 0 + "");
        builder.add("exp", 0 + "");
//        builder.add("userID", "13006314760");
        builder.add("userID",SPUtil.getString(this, "account", ""));
        builder.add("classnum", classNum);
        builder.add("icon", icon + "");
        builder.add("name", newXzmc);
        builder.add("music", newMusic + "");
        builder.add("slogan", newXzkh);
        builder.add("target", newXzmb);
        builder.add("num", teamNum);
        
        Log.i("test", "更新小组时上传给服务器的值是:type=7----id=" + teamId + "monety=0----exp=0----userId=13006314760----classsnum="+ "----Icon=" + icon + classNum + "----newIcon=" + newIcon
                + "----name=" + newXzmc + "----slogan=" + newXzkh + "----taget=" + newXzmb + "----music=" + music + "----num=" + teamNum);
        Request request = new Request.Builder()
                .url(Constant.SERVER_183+"ycsj_jpxgn/servlet/MessageController").post(builder.build())
//                .url("http://www.moregolden.com/ycsj_jpxgn/servlet/MessageController").post(builder.build())
                .build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //关闭创建框
                        UiHelper.dismissDialog();
                        Toast.makeText(CreateTeamActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(final Response response) throws IOException {
            	
            	 final String json = response.body().string();
                 Log.i("test", "修改后返回的json是:" + json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                           
                            JSONObject jsonObject = new JSONObject(json);
                            int state = jsonObject.getInt("state");
                            if (state == 1) {
                                UiHelper.dismissDialog();
                                Toast.makeText(CreateTeamActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else if (state == -1) {
                                //创建小组失败
                                UiHelper.dismissDialog();
                                Toast.makeText(CreateTeamActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    /**
     * 判断用户是否修改了小组信息的方法
     */
    private boolean hasUpdateTeam() {
        //先判断是否修改了小组的相关信息,比较icon和newIcon的值,比较xzmc和newXzmc的值,比较xzkh和newXzkh,比较xzmb和newXzmb,比较music和newMusic的值
        //5者只要有一个不同证明用户修改了小组相关信息,这需要进行请求服务器,将修改信息进行上传
        boolean res = false;
        if (icon != newIcon || !xzmc.equals(newXzmc) || !xzkh.equals(newXzkh) || !xzmb.equals(newXzmb) || music != newMusic) {
            res = true;
        }
        return res;
    }

    /**
     * 创建新小组请求服务器,上传设置的小组的相关信息
     * 创建新小组需要上传的服务器信息有:type,userid(用户id),classnum(班级编号),icon(小组头像),name(小组名称),slogan(小组口号)
     * target(小组目标),music(小组音乐)
     */
    private void httpCreateTeam() {
        //请求服务器,删除该位置学生的信息
        OkHttpClient mOkHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("type", "5");
        builder.add("userID", SPUtil.getString(this, "account", ""));
//        builder.add("userID", "13006314760");
        builder.add("classnum", classNum);
        builder.add("icon", icon + "");
        builder.add("name", xzmc);
        builder.add("slogan", xzkh);
        builder.add("taget", xzmb);
        builder.add("music", music + "");


//        Log.i("test", "创建小组时上传给服务器的值是:type=5----userId=13006314760----classsnum=" + classNum + "----icon=" + icon
//                + "----name=" + xzmc + "----slogan=" + xzkh + "----target=" + xzmb + "----music=" + music);

        Request request = new Request.Builder()
                .url(Constant.SERVER_183+"ycsj_jpxgn/servlet/MessageController").post(builder.build())
//                .url("http://www.moregolden.com/ycsj_jpxgn/servlet/MessageController").post(builder.build())
                .build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //关闭创建框
                        UiHelper.dismissDialog();
                        Toast.makeText(CreateTeamActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(final Response response) throws IOException {
            	final String json = response.body().string();
            	Log.e("test", json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            
                            
                            
                            
                            JSONObject jsonObject = new JSONObject(json);
                            int state = jsonObject.getInt("state");
                            if (state == 1) {
                                UiHelper.dismissDialog();
                                Toast.makeText(CreateTeamActivity.this, "创建成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else if (state == -1) {
                                //创建小组失败
                                UiHelper.dismissDialog();
                                Toast.makeText(CreateTeamActivity.this, "创建小组失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("error", e.toString());
                        }
                    }
                });
            }
        });
    }

    /**
     * 显示选择小组名称的PopUpWindow
     */
    private void showPopCommon(final int pos, String title, final List<String> items) {
        commonView = View.inflate(this, R.layout.pop_commonview, null);
        tvTitle = (TextView) commonView.findViewById(R.id.tv_title);
        lvCommon = (ListView) commonView.findViewById(R.id.lv);
        tvCancel = (TextView) commonView.findViewById(R.id.tv_cancel);

        //设置数据
        tvTitle.setText(title);
        popLvAdapter = new PopLvAdapter(this, items, R.layout.lv_item_pop_common);
        lvCommon.setAdapter(popLvAdapter);

        //设置监听
        lvCommon.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i != items.size() - 1) {
                    //ListView的条目点击事件
                    switch (pos) {
                        case 0:
                            //小组名称
                            if (teamNames.contains(items.get(i))) {
                                Toast.makeText(CreateTeamActivity.this, "小组名称已经被使用", Toast.LENGTH_LONG).show();
                            } else {
                                if (flag == 200) {
                                    //创建
                                    tvXzmc.setText(items.get(i));
                                    xzmc = items.get(i);
                                } else if (flag == 100) {
                                    //修改
                                    tvXzmc.setText(items.get(i));
                                    newXzmc = items.get(i);
                                }
                            }
                            break;
                        case 1:
                            //小组口号
                            if (flag == 200) {
                                tvXzkh.setText(items.get(i));
                                xzkh = items.get(i);
                            } else if (flag == 100) {
                                tvXzkh.setText(items.get(i));
                                newXzkh = items.get(i);
                            }
                            break;
                        case 2:
                            //小组目标
                            if (flag == 200) {
                                tvXzmb.setText(items.get(i));
                                xzmb = items.get(i);
                            } else if (flag == 100) {
                                tvXzmb.setText(items.get(i));
                                newXzmb = items.get(i);
                            }
                            break;
                    }
                } else {
                    //点击了自定义跳转到选择头像界面
                    Intent intent = new Intent(CreateTeamActivity.this, EditMsgActivity.class);
                    intent.putExtra("flag", pos);
                    startActivityForResult(intent, 1000);
                }

                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    commonView = null;
                }
            }
        });
        tvCancel.setOnClickListener(this);

        popupWindow = new PopupWindow(commonView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        backgroundAlpha(this, 0.3f);
        popupWindow.setAnimationStyle(R.style.pop_common_style);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(CreateTeamActivity.this, 1.0f);
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow = null;
                    commonView = null;
                }
            }
        });
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

    /**
     * 点击右箭头同步状态的方法
     */
    private void syncRightState() {
        pos++;
        if (lvLeft.getVisibility() != View.VISIBLE) {
            lvLeft.setVisibility(View.VISIBLE);
        }
        if (pos == icons.length) {
            pos = 9;
            return;
        } else {
            lvRight.setVisibility(View.VISIBLE);
            if (pos == icons.length - 1) {
                lvRight.setVisibility(View.INVISIBLE);
            }
            lvTeam.setImageResource(icons[pos]);
        }
    }

    /**
     * 点击左箭头同步状态的方法
     */
    private void syncLeftState() {
        pos--;
        if (lvRight.getVisibility() != View.VISIBLE) {
            lvRight.setVisibility(View.VISIBLE);
        }

        if (pos < 0) {
            pos = 0;
            return;
        } else {
            lvLeft.setVisibility(View.VISIBLE);
            if (pos == 0) {
                lvLeft.setVisibility(View.INVISIBLE);
            }
            lvTeam.setImageResource(icons[pos]);
        }
    }


    /**
     * 弹出选择小组头像图片的Pop
     */
    private void showPopHead() {
        headView = View.inflate(this, R.layout.pop_item_head, null);
        lvDelete = (ImageView) headView.findViewById(R.id.lv_delete);
        lvLeft = (ImageView) headView.findViewById(R.id.lv_left);
        lvRight = (ImageView) headView.findViewById(R.id.lv_right);
        lvTeam = (ImageView) headView.findViewById(R.id.lv_team);
        tvSave = (TextView) headView.findViewById(R.id.tv_save);
        //初始状态将左边的箭头隐藏,右边的箭头显示
        lvLeft.setVisibility(View.INVISIBLE);
        lvRight.setVisibility(View.VISIBLE);
        lvTeam.setImageResource(icons[pos]);
        lvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                    headView = null;
                }
            }
        });
        lvLeft.setOnClickListener(this);
        lvRight.setOnClickListener(this);
        tvSave.setOnClickListener(this);

        popupWindow = new PopupWindow(headView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        backgroundAlpha(this, 0.3f);
        popupWindow.setAnimationStyle(R.style.AnimationAlphaFade);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(CreateTeamActivity.this, 1.0f);
                //PopUpWindow消失的将选择位置清空
                pos = 0;
            }
        });
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000 && null != data) {
            //获取回传的值
            String content = data.getStringExtra("data");
            switch (resultCode) {
                case 100:
                    //小组名称
                    if (flag == 200) {
                        //创建小组
                        xzmc = content;
                    } else if (flag == 100) {
                        //修改小组
                        newXzmc = content;
                    }
                    tvXzmc.setText(content);
                    break;
                case 101:
                    //小组口号
                    if (flag == 200) {
                        xzkh = content;
                    } else if (flag == 100) {
                        newXzkh = content;
                    }
                    tvXzkh.setText(content);
                    break;
                case 102:
                    //小组目标
                    if (flag == 200) {
                        xzmb = content;
                    } else if (flag == 100) {
                        newXzmb = content;
                    }
                    tvXzmb.setText(content);
                    break;
            }
        } else if (requestCode == 0X140 && null != data) {
            //获取到选择的小组音乐的num
            if (resultCode == 0x110) {
                if (flag == 200) {
                    music = data.getIntExtra("index", -1);
                    if (music != -1) {
                        tvXzyy.setText(music + "");
                    }
                } else if (flag == 100) {
                    newMusic = data.getIntExtra("index", -1);
                    if (newMusic != -1) {
                        tvXzyy.setText(newMusic + "");
                    }
                }
            }
        }
    }

    /**
     * 改变屏幕透明度 方法
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }


    /**
     * 设置沉浸状态栏的方法,需要在setContentView()之后调用
     */
    private void setBar() {
        //兼容5.0+，状态栏设置透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else
            //为了沉浸式状态栏 4.4以上才有
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
    }

}
